#include "Queue.h"
#include "NOW.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


/*// 优先队列结构体
typedef struct {
   int array[MAX_READY_N];  //队列数组
   int maxsize;     //该队列所代表资源的数量
   int size;        //队列中元素个数
   int head;        //队列头
   int tail;        //队列尾 (]的表示关系，head元素不是队列内的
   int * occupy;    //表示当前哪个进程在占用资源
} PriorityQueue;*/

// 初始化队列
int q_init(PriorityQueue *queue, int maxsize) {
    queue->size = 0; 
    queue->head=0;
    queue->tail=0;
    queue->maxsize = maxsize;
    queue->occupy = (int *)malloc(sizeof(int) * maxsize);
    if (queue->occupy == NULL) {
        // 处理内存分配失败的情况
        fprintf(stderr, "Memory allocation failed\n");
        return 0;
    }
    memset(queue->occupy,-1,sizeof(int)*maxsize);
    return 1;
}

int q_full(const PriorityQueue *queue){
    return queue->size == MAX_READY_N-1;
}
// 检查队列是否为空
int q_empty(const PriorityQueue *queue) {
    return queue->size == 0;
}

// 入队操作
void q_push(PriorityQueue *queue, int index, Algorithm alg) {
    queue->size++;  
    // (]
    //这里支持不同算法实现，简化起见就都用的FCFS
    switch (alg){
        case PSA:
            /*if(queue->size==1) {
                queue->tail=(queue->tail+1)%MAX_READY_N;
                queue->array[queue->tail] = index;
                return;
            }
            for(int i=queue->head;i!=queue->tail;i=(i+1)%MAX_READY_N){
                int j=queue->array[(i+1)%MAX_READY_N];
                if(jobs[j].priority<jobs[index].priority) queue->array[i]=queue->array[(i+1)%MAX_READY_N];
                else{
                    queue->array[i]=index;
                    break;
                }
            }
            queue->head=(queue->head-1+MAX_READY_N)%MAX_READY_N;
            break;*/
        case FCFS:
        default:
            queue->tail=(queue->tail+1)%MAX_READY_N;
            queue->array[queue->tail] = index;
    }
        

}


int q_push_occupy(PriorityQueue *queue, int pid){
    for(int i=0;i<queue->maxsize;i++){
        if(queue->occupy[i]==-1){
            queue->occupy[i]=pid;
            return 1;
        }
    }
    return 0;
}

int q_pop_occupy(PriorityQueue *queue, int pid){
    for(int i=0;i<queue->maxsize;i++){
        if(queue->occupy[i]==pid){
            queue->occupy[i]=-1;
            return 1;
        }
    }
    return 0;
}

// 出队操作
int q_pop(PriorityQueue *queue) {
    queue->head++;
    queue->size--;
    return queue->array[queue->head];
}

// 获取队首元素
int q_front(const PriorityQueue *queue) {
    return queue->array[queue->head+1];
}

// 释放队列内存
int q_free(PriorityQueue* queue) {
    if (queue->occupy != NULL) {
        free(queue->occupy);
        queue->occupy = NULL;
        queue->size = 0;
        queue->maxsize = 0;
        return 1;
    }
}    

int q_popout(PriorityQueue* queue,int pid){
    int i;
    for(i=queue->tail;i!=queue->head;i=(i-1+MAX_READY_N)%MAX_READY_N){
        if(queue->array[i]==pid) break;
    }
    if(i==queue->head) return 0;
    while(i!=queue->head){
        int z=(i-1+MAX_READY_N)%MAX_READY_N;
        queue->array[i]=queue->array[z];
        i=z;
    }
    queue->size--;
    queue->head=(queue->head+1)%MAX_READY_N;
    return 1;
} //从队列指定剔除一个元素